/**
 * @author mithun
 */

Event.observe(window, 'load', function(event) {

	switchLocationType($('TelematicUnitTelematicUnitLocationType'));

});	

var telematicUnitTypeHandler = function(el){
	
	var telematicUnitType = el.options[el.selectedIndex].text;
	
	switch(telematicUnitType) {
		case 'dmu':
		case 'tgate':
			$('TelematicUnitTelematicUnitLocationType').selectedIndex = 1;
			switchLocationType($('TelematicUnitTelematicUnitLocationType'));
			break;
		case 'ivtu':
			$('TelematicUnitTelematicUnitLocationType').selectedIndex = 2;
			switchLocationType($('TelematicUnitTelematicUnitLocationType'));
			break;
		default:
			break;
	}

}
	
var switchLocationType = function(el) {
	
	if (el.value == 'V') {
		$('TelematicUnitVehicleId').parentNode.parentNode.show();
		$('TelematicUnitHotzoneId').parentNode.parentNode.hide();
	} else if (el.value == 'H') {
		$('TelematicUnitHotzoneId').parentNode.parentNode.show();
		$('TelematicUnitVehicleId').parentNode.parentNode.hide();
	}
	
}

var checkForm = function(el) {
	
	switch ($('TelematicUnitTelematicUnitLocationType').value) {
		case '':
			alert("Please select a location type");
			return false;
			break;
		case 'V':
			if ($('TelematicUnitVehicleId').value == '') {
				alert("Please select a Vehicle");
				return false;
			}
			break;
		case 'H':
			if ($('TelematicUnitHotzoneId').value == '') {
				alert("Please select a Hotzone");
				return false;
			} 		
			break;
		default:
			alert("Please select a Vehicle / Hotzone");
			return false;
			break;
	}
	
	submitFormViaJavaScript(el.form);
		
	return true;
	
}